import lodash from 'lodash';

const utils = {
    //深度克隆
    cloneDeep(objects){
        const obj = lodash.cloneDeep(objects);
        return obj;
    },
    // 下划线转换驼峰
    toHump(name) {
        return name.replace(/\_(\w)/g, function(all, letter){
            return letter.toUpperCase();
        });
    },
    // 驼峰转换下划线
    toLine(name) {
        return name.replace(/([A-Z])/g,"_$1").toLowerCase();
    },
    fullUuid(){
        var  s = [];
        var  hexDigits =  "0123456789abcdef" ;
        for  ( var  i = 0; i < 36; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] =  "4" ;   // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);   // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] =  "-" ;

        var  uuid = s.join( "" );
        return  uuid;
    },
    uuid(){
        let uuid = this.fullUuid().replaceAll("-", "");
        return uuid;
    },
    // 是否为空值
    isNull(value){
        return value === null || value === undefined;
    },
    // 复制内容至剪切板
    copyToPlate(value){
        try {
            let txtEl = document.createElement('textarea');
            txtEl.value = value;
            document.body.appendChild(txtEl);
            txtEl.select(); // 选择对象
            document.execCommand("Copy"); // 执行浏览器复制命令
            txtEl.remove();
            return true;
        } catch(err) {
            alert('复制至剪切板失败');
            console.error(err);
            return false;
        }
    },
    // 复制内容至剪切板
    copyValue(value){
        return this.copyToPlate(value);
    },
    /**
     * 生成一个不重复的ID
     * @param { Number } randomLength
     */
    randomId(randomLength=3) {
        let dateStr = Date.now().toString().substr(8);
        // console.log("dateStr", dateStr);
        let num = Number(Math.random().toString().substr(2, randomLength) + dateStr + Math.random().toString().substr(2, randomLength));
        // console.log("num: ", num);
        return num.toString(36)
    }
};

/**
 * 是否是数组
 */
export const isArray = function (value) {
    if (typeof Array.isArray === "function") {
        return Array.isArray(value);
    }else{
        return Object.prototype.toString.call(value) === "[object Array]";
    }
};

/**
 * 是否是对象
 */
export const isObject = function (value) {
    return Object.prototype.toString.call(value) === "[object Object]";
};

/**
 * 是否是字符串
 */
export const isString = function (value) {
    return Object.prototype.toString.call(value) === "[object String]";
};



export default utils;



